<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/10 0010
 * Time: 9:04
 */

namespace Cuke\Controllers;


use Cuke\Http\ControllerBase;
use Cuke\Models\CukeAdmin;
use Minho\Captcha\CaptchaBuilder;

/**
 * 用户账户控制器
 * @package Cuke\Controllers
 */
class AccountController extends ControllerBase
{
    /**
     * 显示验证码
     */
    public function captchaAction()
    {
        $captcha = new CaptchaBuilder();

        $captcha->initialize([
            'width' => 150,     // 宽度
            'height' => 50,     // 高度
            'line' => false,    // 直线
            'curve' => true,    // 曲线
            'noise' => 1,       // 噪点背景
        ]);

        $captcha->create();
        $captcha->output(1);
        $this->session->set('captcha',$captcha->getText());
    }

    /**
     * 用户登录
     */
    public function loginAction()
    {
        $url = $this->request->getQuery('url');

        if(empty($url)){
            $url = $this->url->get('home/index');
        }
        $this->view->setVar('redirectUrl',urlencode($url));
        $this->view->setVar('loginName','');

        if($this->request->isPost()){
            $loginName = $this->request->getPost('loginName','trim');
            $password = $this->request->getPost('password');
            $validCode = $this->request->getPost('validCode','trim');
            $remember = $this->request->getPost('remember');

            if(empty($validCode)){
                $this->flashSession->error('验证码不能为空!');
                return;
            }
            if(empty($loginName)){
                $this->flashSession->error('账号不能为空!');
                return;
            }
            if(empty($password)){
                $this->flashSession->error('密码不能为空!');
                return;
            }
            if($this->session->get('captcha') !== $validCode){
                $this->flashSession->error('验证码不正确!');
                $this->view->setVar('loginName',$loginName);
                return;
            }

            $admin = CukeAdmin::findFirst([
                'conditions' => 'account=:account:',
                'bind' => ['account' => $loginName]
                ]);

           if(empty($admin) || !password_verify($password,$admin->password)){
                $this->flashSession->error('用户不存在或密码错误!');
           }else{
               $this->session->set('member',serialize($admin));
               $this->session->set('RoleCode',role_code($admin->role));

               if($remember){
                   $this->cookies->set('cuke_id',$admin->id);
               }
               $url = $this->request->getQuery('url');

               if(empty($url)){
                    $url = $this->url->get('home/index');
               }
                $this->response->redirect($url);
           }
           return;
        }

    }

    /**
     * 退出登录
     */
    public function logoutAction()
    {
        $this->cookies->delete('cuke_id');
        $this->session->destroy(true);
        $this->response->redirect($this->url->get('account/login'));
    }
}